//
//  StaffManagerVC.m
//  LogisticsSupplier
//
//  Created by 吕晶 on 2020/8/31.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "StaffManagerVC.h"
#import "StaffMainModel.h"
#import "StaffListCell.h"
#import "EditStaffVC.h"
#import "AddStaffVC.h"
#import "ModifyStaffVC.h"

@interface StaffManagerVC () <UITableViewDelegate,UITableViewDataSource>{
    int page;
}

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) NSMutableArray <StaffMainModel *>*array;

@end

@implementation StaffManagerVC

-(NSMutableArray<StaffMainModel *> *)array {
    if (!_array) {
        _array = [NSMutableArray array];
    }
    return _array;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH - NaviHeight - 64 - BottomHeight)];
        _tableView.backgroundColor = Color_backgroundColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        __weak_self;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            __strong_self;
            strongSelf->page = 1;
            [strongSelf requestForList];
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            __strong_self;
            strongSelf->page++;
            [strongSelf requestForList];
        }];
        _tableView.ly_emptyView = [LYEmptyView emptyViewWithImageStr:@"img_occupy"
             titleStr:@""
            detailStr:@""];
    }
    return _tableView;
}

-(UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenH - BottomHeight - NaviHeight - 64, ScreenW, 64+BottomHeight)];
        _bottomView.backgroundColor = HexColor(0xffffff);
        
        UIButton *saveBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 10, ScreenW - 60, 44)];
        [saveBtn setBackgroundColor:Color_ThemeColor];
        saveBtn.layer.masksToBounds = YES;
        saveBtn.layer.cornerRadius = 6;
        [saveBtn setTitle:@"添加员工" forState:0];
        [saveBtn setTitleColor:HexColor(0xffffff) forState:0];
        saveBtn.titleLabel.font = BoldFontOfSize(16);
        [saveBtn addTarget:self action:@selector(addClick) forControlEvents:UIControlEventTouchUpInside];
        [_bottomView addSubview:saveBtn];
    }
    return _bottomView;
}

- (void)addClick {
    AddStaffVC *vc = [AddStaffVC new];
    [self.rt_navigationController pushViewController:vc animated:YES];
}

- (void)requestForList {
    [HttpRequest PostWithApi:LogisticsSupplierStaffList params:@{@"page":@(page)} progress:^(NSProgress * _Nonnull progress) {

    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            if (self->page == 1) {
                [self.array removeAllObjects];
                self.array = [StaffMainModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
            }else {
                NSArray *array = [StaffMainModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
                [self.array addObjectsFromArray:array];
            }
            BOOL has_more = [responseObject[@"has_more"] boolValue];
            if (has_more) {
                self.tableView.mj_footer.hidden = NO;
            }else {
                self.tableView.mj_footer.hidden = YES;
            }
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
        }
    } fail:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"员工管理";
    page = 1;
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.bottomView];
    UIButton *editBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 20)];
    [editBtn setTitle:@"编辑" forState:0];
    [editBtn setTitleColor:HexColor(0x9C9DB3) forState:0];
    editBtn.titleLabel.font = BoldFontOfSize(14);
    [editBtn addTarget:self action:@selector(editClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:editBtn];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    page = 1;
    [self requestForList];
}

- (void)editClick {
    EditStaffVC *vc = [EditStaffVC new];
    [self.rt_navigationController pushViewController:vc animated:YES];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    StaffMainModel *model = self.array[indexPath.row];
    StaffListCell *cell = [StaffListCell StaffListCellWithTableView:tableView];
    cell.model = model;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    StaffListCell *cell = [StaffListCell StaffListCellWithTableView:tableView];
    return cell.height;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    StaffMainModel *model = self.array[indexPath.row];
    if (![model.isadmin isEqualToString:@"1"]) {
        ModifyStaffVC *vc = [ModifyStaffVC new];
        vc.sw_id = model.sw_id;
        [self.rt_navigationController pushViewController:vc animated:YES];
    }
}

@end
